package boy.ui
{
   import flash.display.Bitmap;
   import flash.display.BitmapData;
   import flash.display.Sprite;
   import flash.display.Stage;
   import flash.events.Event;
   import flash.events.MouseEvent;
   import flash.ui.Mouse;
   import flash.ui.MouseCursor;
   
   /**
    * Runtime Versions: FlashPlayer 10.0
    * @author cjboy | cjboy1984@gmail.com
    * @usage
    // init
    CursorManager_10_0.init(stage);
    
    // show grab open
    CursorPlusV1.cursor = CursorPlusV1.GRAB_OPEN;
    // show grab closed
    CursorPlusV1.cursor = CursorPlusV1.GRAB_CLOSED;
    // return to normal
    CursorPlusV1.cursor = CursorPlusV1.AUTO;
    
    // destroy
    CursorPlusV1.destroy();
    */
   public class CursorPlusV1 extends Sprite
   {
      [Embed(source = 'png/grab_closed.png')]
      private const BMPDATA_CLOSED:Class;
      [Embed(source = 'png/grab_open.png')]
      private const BMPDATA_OPEN:Class;
      
      public static const GRAB_CLOSED:String = 'grab_closed';
      public static const GRAB_OPEN:String = 'grab_open';
      public static const ARROW:String = MouseCursor.ARROW;
      public static const AUTO:String = MouseCursor.AUTO;
      public static const BUTTON:String = MouseCursor.BUTTON;
      public static const HAND:String = MouseCursor.HAND;
      public static const IBEAM:String = MouseCursor.IBEAM;
      
      // singleton
      private static var instance:CursorPlusV1;
      
      // bmp
      private var bmp_grab_closed:Bitmap;
      private var bmp_grab_open:Bitmap;
      
      public function CursorPlusV1()
      {
         mouseEnabled = false;
         mouseChildren = false;
         tabEnabled = false;
         tabChildren = false;
         focusRect = false;
         
         bmp_grab_closed = new BMPDATA_CLOSED();
         bmp_grab_closed.x = -bmp_grab_closed.width / 2;
         bmp_grab_closed.y = -bmp_grab_closed.height / 2;
         bmp_grab_closed.visible = false;
         addChild(bmp_grab_closed);
         
         bmp_grab_open = new BMPDATA_OPEN();
         bmp_grab_open.x = -bmp_grab_open.width / 2;
         bmp_grab_open.y = -bmp_grab_open.height / 2;
         bmp_grab_open.visible = false;
         addChild(bmp_grab_open);
         
         addEventListener(Event.ADDED_TO_STAGE, onAdd);
         addEventListener(Event.REMOVED_FROM_STAGE, onRemove);
      }
      
      // ________________________________________________
      //                                           Static
      
      public static function init(pStage:Stage):void
      {
         if (!instance)
         {
            instance = new CursorPlusV1();
         }
         if (!pStage.contains(instance))
         {
            pStage.addChild(instance);
         }
      }
      
      public static function destroy():void
      {
         if (!instance) return;
         
         instance.hide();
      }
      
      public static function set cursor(v:String):void
      {
         if (!instance)
         {
            trace('Err : should call CursorPlusV1.init(stage); at first.');
            return;
         }
         
         switch(v)
         {
            case GRAB_CLOSED:
               instance.showGrabClosed();
               break;
            case GRAB_OPEN:
               instance.showGrabOpen();
               break;
            case ARROW:
            case AUTO:
            case BUTTON:
            case HAND:
            case IBEAM:
               instance.hide();
               Mouse.cursor = v;
               break;
            default:
               instance.hide();
               Mouse.cursor = MouseCursor.AUTO;
               break;
         }
      }
      
      // ________________________________________________
      //                                           Normal
      
      public function showGrabOpen():void
      {
         if (!parent) return;
         if (!stage) return;
         if (parent != stage) return;
         
         updateMouse();
         bmp_grab_closed.visible = false;
         bmp_grab_open.visible = true;
         
         Mouse.hide();
         
         stage.addEventListener(MouseEvent.MOUSE_MOVE, updateMouse);
      }
      
      public function showGrabClosed():void
      {
         if (!parent) return;
         if (!stage) return;
         if (parent != stage) return;
         
         updateMouse();
         bmp_grab_closed.visible = true;
         bmp_grab_open.visible = false;
         
         Mouse.hide();
         
         stage.addEventListener(MouseEvent.MOUSE_MOVE, updateMouse);
      }
      
      public function hide():void
      {
         bmp_grab_closed.visible = false;
         bmp_grab_open.visible = false;
         
         Mouse.show();
         
         stage.removeEventListener(MouseEvent.MOUSE_MOVE, updateMouse);
      }
      
      // ################### protected ##################
      
      // #################### private ###################
      
      private function onAdd(e:Event):void
      {
      }
      
      private function onRemove(e:Event):void
      {
         hide();
      }
      
      // --------------------- LINE ---------------------
      
      private function updateMouse(e:MouseEvent = null):void
      {
         stage.setChildIndex(this, stage.numChildren - 1);
         x = stage.mouseX;
         y = stage.mouseY;
         
         if (e)
         {
            e.updateAfterEvent();
         }
      }
      
      // --------------------- LINE ---------------------
      
   }
   
}